package com.cby.operation.anti

import android.os.Handler
import android.os.Looper
import com.cby.operation.anti.bean.PopConfigBean
import com.cby.operation.common.util.CbyLogUtils.flog
import com.cby.operation.initialize.InitManager
import java.util.*

/**
 * Description:心跳管理
 *
 * @author 陈宝阳
 * @create 2020/1/2 17: 24
 */
class AntiHeartManager private constructor() {

    private val mHandle = Handler(Looper.myLooper()!!)

    /**
     * 是否暂停
     */
    private var isPause = false

    /**
     * 当前毫秒数
     */
    var currentSecond = 0L
        private set

    /**
     * 心跳的时间间隔
     */
    private var interval = 0

    /**
     * 倒计时的剩余时间
     */
    private var remainingTime = -1

    /**
     * 倒计时的提示时间点
     */
    private val moments: MutableList<PopConfigBean> = ArrayList()

    /**
     * 判断是否运行了
     */
    private var isRunning = false

    /**
     * 监听
     */
    private var listener: HeartListener? = null

    companion object{
        val instance = SingletonHolder.instance
    }

    private object SingletonHolder {
        /**
         * 返回HykbFcmHeartManager对象
         */
        val instance = AntiHeartManager()
    }

    fun setPause(isPause: Boolean) {
        this.isPause = isPause
        if (!isPause) {
            mHandle.post(timeRunnable)
        }
    }

    /**
     * 设置心跳监听
     *
     * @param listener 回调
     */
    fun setHeartListener(listener: HeartListener?) {
        this.listener = listener
    }

    /**
     * 开始心跳
     *
     * @param interval 心跳间隔时长
     */
    fun startHeart(interval: Int) {
        this.interval = interval
        if (isRunning) {
            return
        }
        mHandle.post(timeRunnable)
        isRunning = true
    }

    /**
     * 设置剩余的时间和倒计时弹窗的时间点
     */
    fun setRemainingTime(remainingTime: Int, moments: List<PopConfigBean>?) {
        this.remainingTime = remainingTime
        if (moments != null) {
            this.moments.addAll(moments)
        }
    }

    /**
     * 停止心跳
     */
    fun stopHeart() {
        mHandle.removeCallbacks(timeRunnable)
        isRunning = false
    }

    private val timeRunnable: Runnable = object : Runnable {
        override fun run() {
            if (interval <= 0) {
                mHandle.removeCallbacks(this)
                isPause = true
                return
            }
            currentSecond++
            if (currentSecond >= interval && currentSecond % interval == 0L && listener != null) {
                //心跳回调
                listener!!.onBeat(currentSecond)
            }
            if (remainingTime != -1) {
                remainingTime--
            }
            for (i in moments.indices) {
                val bean = moments[i] ?: continue
                if (bean.moment == remainingTime && listener != null) {
                    flog("倒计时 -->$remainingTime")
                    //检查是否到倒计时弹窗或者气泡的点
                    listener!!.onCountDown(bean)
                    break
                }
            }
            if (remainingTime <= 0 && listener != null) {
                //倒计时结束，心跳回调, 请求服务端
                listener!!.onBeat(currentSecond)
                mHandle.removeCallbacks(this)
                isPause = true
            }

            if (!isPause) {
                //递归调用本runnable对象，实现每隔一秒一次执行任务,进行计时
                mHandle.postDelayed(this, 1000)
            }
        }
    }

    interface HeartListener {
        fun onBeat(time: Long)
        fun onCountDown(bean: PopConfigBean?)
    }
}